$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/jxcourse/list',
        datatype: "json",
        colModel: [			
			{ label: 'id', name: 'id', index: 'id', width: 50, key: true },
			{ label: '课程名称', name: 'name', index: 'name', width: 80 }, 			
			{ label: '课程分类', name: 'typeName', index: 'typeName', width: 80 },
			{ label: '宣传图', name: 'imgUrl', index: 'img_url', width: 80 ,formatter:function (cellvalue, options, rowObject) {
                    return "<img src="+cellvalue+" style='width: 150px; height: 80px' />";
                }},
			{ label: '课程简介', name: 'courseIntroduction', index: 'course_introduction', width: 80 },
            {label: '操作', name: 'id', index: 'id', width: 80 ,formatter:function(cellvalue, options, rowObject){

                    var text="<a class='btn btn-default ' onclick='showfile("+cellvalue+")' ><i class='fa fa-file-word-o' style='font-size: 20px'></i>&nbsp;资料管理</a>";
                    return text;
                }}
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        shrinkToFit:true,
        autoScroll: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	//$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "scroll" });
        }
    });

    Simditor.locale = 'zh-CN';
    toolbar = [ 'title', 'bold', 'italic', 'underline', 'strikethrough',
        'color', 'fontScale', 'ol', 'ul', 'blockquote', 'code', 'table',
        'link', 'image', 'hr',  'indent', 'outdent','alignment' ];

    editor = new Simditor({   // 富文本
        textarea: $('#editor'),
        placeholder: '这里输入文字...',
        toolbar: toolbar,
        allowedTags:['br', 'span', 'a', 'img', 'b', 'strong', 'i', 'strike', 'u', 'font', 'p', 'ul', 'ol', 'li', 'blockquote', 'pre', 'code', 'h1', 'h2', 'h3', 'h4', 'hr'],
        pasteImage: true,
        defaultImage: '/simditor/images/image.png',
        upload: {
            url:baseURL + 'sys/oss//edit/upload',
            params: null,
            fileKey: "file",
            connectionCount: 1,
            leaveConfirm: "正在上传,确定要取消上传文件吗?"}

    });


    new AjaxUpload('#upload', {
        action: baseURL + "sys/oss/upload",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('请上传正确的图片格式!');
                return false;
            }
            layer.load('努力上传中', {
                shade: [0.3,'#fff'] //0.1透明度的白色背景
            });

        },
        onComplete : function(file, r){
            layer.closeAll('loading');
            if(r.code == 0){
                /*alert(r.url);*/
                vm.jxCourse.imgUrl=r.url;
                $("#img").attr("src",r.url);

            }
        }
    });
    vm.imgUrl=$("#img").attr("src");
});
var loading;  // 缓冲效果
var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: -1
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		jxCourse: {},
        imgUrl:"",
	},
	methods: {
        getjxCourseType: function(){
            //加载课程树
            $.get(baseURL + "sys/jxcoursetype/select", function(r){
                ztree = $.fn.zTree.init($("#jxCourseTree"), setting, r.jxCourseList);
                var node = ztree.getNodeByParam("id", 0);
                ztree.selectNode(node);
            })
        },
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.jxCourse = {"typeName":"请选择课程分类"};
			vm.getjxCourseType();
            editor.setValue("");
            $("#img").attr("src",vm.imgUrl);
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
            vm.getjxCourseType();
		},
		saveOrUpdate: function (event) {
            vm.jxCourse.courseDesc=editor.getValue();
            console.log(vm.jxCourse);
		    $('#btnSaveOrUpdate').button('loading').delay(1000).queue(function() {
                var url = vm.jxCourse.id == null ? "sys/jxcourse/save" : "sys/jxcourse/update";
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: 'application/x-www-form-urlencoded',
                    dataType:'json',
                    data: vm.jxCourse,
                    success: function(r){
                        if(r.code === 0){
                             layer.msg("操作成功", {icon: 1});
                             vm.reload();
                             $('#btnSaveOrUpdate').button('reset');
                             $('#btnSaveOrUpdate').dequeue();
                        }else{
                            layer.alert(r.msg);
                            $('#btnSaveOrUpdate').button('reset');
                            $('#btnSaveOrUpdate').dequeue();
                        }
                    }
                });
			});
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			var lock = false;
            layer.confirm('确定要删除选中的记录？', {
                btn: ['确定','取消'] //按钮
            }, function(){
               if(!lock) {
                    lock = true;
		            $.ajax({
                        type: "POST",
                        url: baseURL + "sys/jxcourse/delete",
                        contentType: "application/json",
                        data: JSON.stringify(ids),
                        success: function(r){
                            if(r.code == 0){
                                layer.msg("操作成功", {icon: 1});
                                $("#jqGrid").trigger("reloadGrid");
                            }else{
                                layer.alert(r.msg);
                            }
                        }
				    });
			    }
             }, function(){
             });
		},
		getInfo: function(id){
			$.get(baseURL + "sys/jxcourse/info/"+id, function(r){
                vm.jxCourse = r.jxCourse;
                $("img").attr("src",vm.jxCourse.imgUrl);
                console.log(vm.jxCourse.courseDesc);
                editor.setValue(vm.jxCourse.courseDesc);
                vm.getCourseTypeInfo(vm.jxCourse.courseTypeId);
            });
		},
        getCourseTypeInfo:function(courseTypeId){    //  得到课程分类信息
            $.get(baseURL + "sys/jxcoursetype/info/"+courseTypeId, function(r){
                vm.jxCourse.typeName = r.jxCourseType.name;
            });
        },
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                page:page
            }).trigger("reloadGrid");
		},
        jxCourseTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择课程",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#jxCourseLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.jxCourse.courseTypeId = node[0].id;
                    vm.jxCourse.typeName = node[0].name;
                    layer.close(index);
                }
            });
        },
	}
});
function showfile(id){  // 显示文件上传界面
    window.location.href="jxstudyfile.html?id="+id;
}

function showCoruseSchedule(id) {  //  显示排课界面
    window.location.href="jxcomputydevice.html?id="+id+"&&num="+num;
}